package org.one.stone.soup.grfxML;

public class IPrimativeHelper {
private IPrimativeHelper() {
	super();
}

public static String togrfxML(IPrimative field,String name) {
	return togrfxML(null,field,name,field.getModifier());
}

public static String togrfxML(IPrimative field,String name,TagModifier modifier) {
	return togrfxML(null,field,name,modifier);
}

public static String togrfxML(IPrimative defaultField,IPrimative field,String name,TagModifier modifier) {

	if(defaultField==null)
	{
		try{
			defaultField = (IPrimative)field.getClass().newInstance();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	if(modifier==null)
	{
		modifier = field.getModifier();
	}
	boolean hasData = false;

	String xml = "<" + name;

	if(!NameHelper.isSystemName(field.getId()))
	{
		hasData = true;
		xml += " id=\""+field.getId()+"\"";
	}

	for (int loop = 0; loop < field.getModifier().getAttributeCount(); loop++) {
		if( !(field.getValue(field.getModifier().getAttributeName(loop)).equals(defaultField.getValue(field.getModifier().getAttributeName(loop)))) )
		{
			hasData = true;
			xml += " " + modifier.getAttributeAlias(loop) + "=\"" + field.getValue(modifier.getAttributeName(loop)) + "\"";
		}
	}
	xml += ">";
	xml += "</" + name + ">\n";

	if(hasData==true)
	{
		return xml;
	}
	else
	{
		return "";
	}
}
public static String toString(IPrimative field) {

//	if (GrfxMLEngine.DEBUG)
//		return (togrfxML(field,field.getModifier().DEFAULT_ID.substring(1)));

	String xml = "<" + field.getModifier().DEFAULT_ID.substring(1).toLowerCase();

	if(!NameHelper.isSystemName(field.getId()))
	{
		xml += " id=\""+field.getId()+"\"";
	}

	for (int loop = 0; loop < field.getModifier().getAttributeCount(); loop++) {
		xml += " " + field.getModifier().getAttributeAlias(loop) + "=\"" + field.getValue(field.getModifier().getAttributeName(loop)) + "\"";
	}
	xml += "/>";

	return xml;
}
}